package cn.buk.tms.misc;

import cn.buk.tms.common.dto.flight.BaseFlightOrderPassengerDto;
import cn.buk.tms.flight.dto.response.FlightOrderResponseDto;
import cn.buk.tms.report.service.BaseReportExportExcel;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Service;

import static cn.buk.common.Constant.*;
import static cn.buk.tms.common.constant.TmsConstant.ID_TYPE_NI;
import static cn.buk.tms.common.constant.TmsConstant.ID_TYPE_PASSPORT;

@Service
public class ExportExcelServiceImpl extends BaseReportExportExcel implements ExportExcelService {

  @Override
  public void exportFlightOrderPassenger(final int enterpriseId, final FlightOrderResponseDto dto, final Sheet sheet,
                                         final String carrierCode) {
    if ("HO".equalsIgnoreCase(carrierCode)) {
      exportHO(enterpriseId, dto, sheet, carrierCode);
    } else if ("MU".equalsIgnoreCase(carrierCode)) {
      exportMU(enterpriseId, dto, sheet, carrierCode);
    } else {
      exportHO(enterpriseId, dto, sheet, carrierCode);
    }
  }

  private void exportMU(final int enterpriseId, final FlightOrderResponseDto dto, final Sheet sheet,
                        final String carrierCode) {
    //标题行
    int currentRowNo = 1;
    createTitleRow(carrierCode + "团客名单", sheet, currentRowNo, 0, 8);

    //表头行
    currentRowNo++;
    createTableHeaderRow(sheet, currentRowNo, "序号", "姓名", "旅客类型", "证件类型", "证件号码", "儿童出生日期", "性别", "手机号");
    setColumnWidth(sheet, 10, 20, 10, 10, 30, 10, 10, 20);

    int sn = 0;
    for (var psg : dto.getPassengers()) {
      sn++;
      currentRowNo++;
      Row row = getRow(sheet, currentRowNo);
      int col = -1;
      col++;
      row.createCell(col, CellType.STRING).setCellValue(sn + "");
      col++;
      row.createCell(col, CellType.STRING).setCellValue(psg.getName());
      col++;
      row.createCell(col, CellType.STRING).setCellValue(processPsgType(psg, carrierCode));
      col++;
      row.createCell(col, CellType.STRING).setCellValue(processIdType(psg, carrierCode));
      col++;
      row.createCell(col, CellType.STRING).setCellValue(psg.getIdNo());
      col++;
      row.createCell(col, CellType.STRING).setCellValue("");
      col++;
      row.createCell(col, CellType.STRING).setCellValue("");
      col++;
      row.createCell(col, CellType.STRING).setCellValue(psg.getMobile());
    }
  }

  private String processPsgType(BaseFlightOrderPassengerDto psg, String carrierCode) {
    if ("HO".equalsIgnoreCase(carrierCode)) {
      if (psg.getPsgType() == PSG_TYPE_ADU) {
        return "0";
      } else if (psg.getPsgType() == PSG_TYPE_CHD) {
        return "1";
      } else if (psg.getPsgType() == PSG_TYPE_INF) {
        return "2";
      }
    } else if ("MU".equalsIgnoreCase(carrierCode)) {
      if (psg.getPsgType() == PSG_TYPE_ADU) {
        return "成人";
      } else if (psg.getPsgType() == PSG_TYPE_CHD) {
        return "儿童";
      } else if (psg.getPsgType() == PSG_TYPE_INF) {
        return "婴儿";
      }
    }
    return "";
  }

  private void exportHO(final int enterpriseId, final FlightOrderResponseDto dto, final Sheet sheet,
                        final String carrierCode) {
    //标题行
    int currentRowNo = 1;
    createTitleRow(carrierCode + "团客名单", sheet, currentRowNo, 0, 6);

    //表头行
    currentRowNo++;
    createTableHeaderRow(sheet, currentRowNo, "序号", "旅客姓名", "旅客类型", "证件类型", "证件号码", "手机号");
    setColumnWidth(sheet, 10, 20, 10, 10, 30, 20);

    int sn = 0;
    for (var psg : dto.getPassengers()) {
      sn++;
      currentRowNo++;
      Row row = getRow(sheet, currentRowNo);
      int col = -1;
      col++;
      row.createCell(col, CellType.STRING).setCellValue(sn + "");
      col++;
      row.createCell(col, CellType.STRING).setCellValue(psg.getName());
      col++;
      row.createCell(col, CellType.STRING).setCellValue(processPsgType(psg, carrierCode));
      col++;
      row.createCell(col, CellType.STRING).setCellValue(processIdType(psg, carrierCode));
      col++;
      row.createCell(col, CellType.STRING).setCellValue(psg.getIdNo());
      col++;
      row.createCell(col, CellType.STRING).setCellValue(psg.getMobile());
    }
  }

  private String processIdType(BaseFlightOrderPassengerDto psg, String carrierCode) {
    if ("HO".equalsIgnoreCase(carrierCode)) {
      if (psg.getIdType() == ID_TYPE_NI) {
        return "0";
      } else if (psg.getIdType() == ID_TYPE_PASSPORT) {
        return "1";
      } else {
        return "3";
      }
    } else if ("MU".equalsIgnoreCase(carrierCode)) {
      if (psg.getIdType() == ID_TYPE_NI) {
        return "公民身份证";
      } else if (psg.getIdType() == ID_TYPE_PASSPORT) {
        return "护照";
      }
    }
    return "";
  }


}
